Dynamic XML Documents

In our previous example, we dynamically created an XML Data Island within the Active Server Page that we were requesting.  In our next example, we are going to create a complete, standalone XML document using the same technique.  Most developers who use Active Server Pages have only developed ASP’s for returning HTML to a browser.  However, there is nothing keeping us from returning an XML document instead.  Let’s look at an example.

First, let’s look at the source code.  Remember, this ASP returns no HTML.  Instead, it returns an XML Document to the client (e.g. the browser).

<%@ Language=VBScript %>
<?xml version="1.0" standalone="yes"?>
<!DOCTYPE employees [
    <!ELEMENT employees ( employee+ )>

    <!ELEMENT employee (name, position, address, phone)>
    <!ATTLIST employee id ID #REQUIRED>

    <!ELEMENT name (first, last)>
    <!ELEMENT first (#PCDATA)>
    <!ELEMENT last  (#PCDATA)>

    <!ELEMENT position (#PCDATA)>

    <!ELEMENT address (street?, city?, state?, zip?)>
    <!ELEMENT street (#PCDATA)>
    <!ELEMENT city   (#PCDATA)>
    <!ELEMENT state  (#PCDATA)>
    <!ELEMENT zip    (#PCDATA)>

    <!ELEMENT phone (main,office*,fax*,mobile*,home*)>
    <!ELEMENT main   (#PCDATA)>
    <!ELEMENT office (#PCDATA)>
    <!ELEMENT fax    (#PCDATA)>
    <!ELEMENT mobile (#PCDATA)>
    <!ELEMENT home   (#PCDATA)>
]>
<employees>
<%
    Dim oConn    ' ADO connection object.
    Dim oRs        ' Employee recordset object.
    Dim oRsPhone    ' Emp phone recordset object.
    Dim strSql    ' SQL string

    ' Establish a connection to the database.    
    Set oConn = Server.CreateObject("ADODB.Connection")
    Call oConn.Open( "DSN=EmployeesMDB;UID=Admin;PWD=" )
    
    ' Open a recordset containing all of the employees.
    strSql = "SELECT * FROM employees " & 
			 "ORDER BY lastname, firstname"
    Set oRs = Server.CreateObject( "ADODB.Recordset" )
    Call oRs.Open( strSql, oConn, adOpenStatic )

    ' Iterate through all of the employees in
    ' the database and generate the appropriate
    ' XML document that contains their information.    
    While Not oRs.EOF

        ' Display all of the employee information
        ' that is in the employee table.
        Response.Write( "<employee id=""" & oRs("id") & 
			""">" & vbCrLf )
        Response.Write( "    <name>" & vbCrLf )
        Response.Write( "        <first>" & 
			oRs("firstname") & "</first>" & vbCrLf )
        Response.Write( "        <last>" & 
			oRs("lastname") & "</last>" & vbCrLf )
        Response.Write( "    </name>" & vbCrLf )
        Response.Write( "    <position>" & 
			oRs("position") & "</position>" & vbCrLf )
        Response.Write( "    <address>" & vbCrLf )
        Response.Write( "        <street>" & 
			oRs("street") & "</street>" & vbCrLf )
        Response.Write( "        <city>" & oRs("city") 
			& "</city>" & vbCrLf )
        Response.Write( "        <state>" & 
			oRs("state") & "</state>" & vbCrLf )
        Response.Write( "        <zip>" & oRs("zip") & 
			"</zip>" & vbCrLf )
        Response.Write( "    </address>" & vbCrLf )
        Response.Write( "    <phone>" & vbCrLf )
        
        ' Get the phone numbers for this employee
        ' and generate the appropriate tags and 
		  ' content.
        Set oRsPhones = 
			Server.CreateObject( "ADODB.Recordset" )
        Call oRsPhones.Open( 
			"SELECT * FROM phonenumbers WHERE id = '" & 
			oRs("id") & "'", oConn )
    
        While Not oRsPhones.EOF
            ' Generate the tag containing the 
			' appropriate
            ' phone information (type and number)
            Response.Write( "        <" & 
				oRsPhones("phonetype") & ">" )
            Response.Write( oRsPhones("phonenumber") )
            Response.Write( "</" & 
			    oRsPhones("phonetype") & ">" & vbCrLf)
            oRsPhones.MoveNext
        Wend
        oRsPhones.Close
        
        Response.Write( "    </phone>" & vbCrLf )
        Response.Write( "</employee>" & vbCrLf & vbCrLf )
        
        oRs.MoveNext 
    Wend
    
    oRs.Close
    oConn.Close 
%>
</employees>

File: Chapter 9\Employees.asp

This code looks very similar to the previous example in that it uses Microsoft’s ActiveX Data Objects (ADO) and queries the database for the content, which it then uses to dynamically create a fully valid XML Document (notice the DTD at the top).  

If the underlying data in the database ever changes, it will be reflected next time the document is requested.  Let’s look at the result.

File: Chapter 9\Employees.asp (requested from IIS).

Notice that the URL is not requesting a file, but is instead using the local web server (in this case IIS) to retrieve the document.  This is because the ASP must be executed within the context of the web server for it to be processed.

This is all very useful, but viewing an XML Document within a browser isn’t always the optimal choice.  But what if we could request this document through client-side scripting using the XML DOM?  We can!  This is what it looks like:

xmlDoc = new ActiveXObject( "microsoft.xmldom" );
xmlDoc.load("http://www.davidharding.com/getdoc.asp");

Notice that in the load() method of the XML DOM object, we are requesting a URL that specifies an ASP page, not a static XML document.  As far as the XML DOM object is concerned, it is just XML.  It does not care were it came from.

Dynamic XML and the Internet

As you can see, creating ASP’s that dynamically generate XML is a great way to pass data from the server to the client.  You can probably think of a lot of applications for this capability.  For instance, you could create a complete data enabled interface to your underlying database using Internet Information Server (IIS) and Active Server Pages (ASP’s) that return XML.  Expose these pages to the Internet, and you can create a complete data enabled interface to any number of applications that use your database(s).

Using ASP’s (or CGI’s in general) and XML, you can provide access to any number of internal applications and their data, which can be made accessible within your organization and to strategic partners.  A common use of this type of access is Business to Business (B2B) eCommerce.